<template>
  <div class="container">
    <div class="title">
      <span>添加分类</span> <span class="back" @click="back"> <i class="iconfont icon-fanhui"></i> 返回 </span>
    </div>
    <div class="wrap">
      <el-row>
        <el-col :lg="16" :md="20" :sm="24" :xs="24">
          <el-form :model="form" status-icon ref="form" label-width="100px" @submit.native.prevent>
            <el-form-item label="分类名称" prop="asw_type_name">
              <el-input size="medium" v-model="form.asw_type_name" placeholder="请填写分类名称"></el-input>
            </el-form-item>
            <el-form-item label="分类图片" prop="asw_type_img">
              <el-input size="medium" v-model="form.asw_type_img" placeholder="请填写分类图片"></el-input>
            </el-form-item>
            <el-form-item label="分类排序" prop="asw_type_sort">
              <el-input size="medium" v-model="form.asw_type_sort" placeholder="请填写分类排序"></el-input>
            </el-form-item>
            <el-form-item class="submit">
              <el-button type="primary" @click="submitForm('form')" :loading="loading">保 存</el-button>
              <el-button @click="resetForm('form')">重 置</el-button>
            </el-form-item>
          </el-form>
        </el-col>
      </el-row>
    </div>
  </div>
</template>

<script>
import asw from '@/model/problem.js'

export default {
  data() {
    return {
      form: {
        mid: '',
        asw_type_name: '',
        asw_type_img: '',
        asw_type_sort: '',
      },
      loading: false,
    }
  },
  methods: {
    async submitForm(formName) {
      try {
        this.loading = true
        const res = await asw.createType(this.form)
        console.log(res)
        this.loading = false
        if (res.code < window.MAX_SUCCESS_CODE) {
          this.$message.success(`${res.message}`)
          this.resetForm(formName)
        }
      } catch (error) {
        this.loading = false
        this.$message.error('分类添加失败，请检测填写信息')
        console.log(error)
      }
    },
    // 重置表单
    resetForm(formName) {
      this.$refs[formName].resetFields()
    },
    back() {
      this.$emit('addClose')
    },
  },
}
</script>

<style lang="scss" scoped>
  .container {
    .title {
      height: 59px;
      line-height: 59px;
      color: $parent-title-color;
      font-size: 16px;
      font-weight: 500;
      text-indent: 40px;
      border-bottom: 1px solid #dae1ec;
    }

    .back {
      float: right;
      margin-right: 40px;
      cursor: pointer;
    }

    .wrap {
      padding: 20px;
    }

    .submit {
      float: left;
    }
  }
</style>
